import { _decorator, Component } from 'cc';
import Cryptor, { aes_decrypt, aes_encrypt, base64_decode, base64_encode, md5 } from '../../sgame/encrypt/Cryptor';
import { llog } from '../../sgame/logger/Logger';
const { ccclass, property } = _decorator;

@ccclass('Main')
export class Main extends Component {
    start() {
        const data: string = "123456";
        llog(`data: ${data}, md5: ${md5(data)}`);
        llog(`data: ${data}, sha256: ${Cryptor.SHA256(data)}`);

        const key: string = "sgame_key";
        const iv: string = "sgame_iv";
        const aesEncrypted: string = aes_encrypt(data, key, iv);
        llog(`data: ${data}, aesEncrypt: ${aesEncrypted}`);
        llog(`data: ${aesEncrypted}, aesDecrypt: ${aes_decrypt(aesEncrypted, key, iv)}`);

        Cryptor.Init(key, iv);
        llog(`Cryptor.Init(${key}, ${iv})`)
        const aesEncrypted2: string = aes_encrypt(data);
        llog(`data: ${data}, aesEncrypt: ${aesEncrypted}`);
        llog(`data: ${aesEncrypted2}, aesDecrypt: ${aes_decrypt(aesEncrypted2)}`);

        // 使用 btoa 来进行base64编码不能解析utf8编码的字符串
        // const data2 = '包含中文字符串123456abcdefg';
        // const base64ed = btoa(data2);
        // llog(`data2: ${data2}, base64: ${base64ed}`)
        // llog(`base64ed: ${base64ed}, base64Decode: ${atob(base64ed)}`)

        const data2 = '包含中文字符串123456abcdefg';
        const base64ed = base64_encode(data2);
        llog(`data2: ${data2}, base64: ${base64ed}`)
        llog(`base64ed: ${base64ed}, base64Decode: ${base64_decode(base64ed)}`)
    }
}